package com.vtuzx.kingdee.xqc_core.dao;

import com.vtuzx.core.db.mongo.impl.VtuzxDaoImpl;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_core.constant.BillStatus;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Component
public class PrdTaskDetailDao extends VtuzxDaoImpl {
    /** 集合名称 */
    public static final String NAME = "t_prd_task_detail";

    public PrdTaskDetailDao() {
        super(NAME);
    }

    /**
     * 新建单据
     *
     * @param _userId 用户ID
     * @param userName 用户名
     * @param doc 数据
     * @return 单据ID
     */
    public ObjectId insertBill(ObjectId _userId, String userName, Document doc) {
        doc.append("_createUserId", _userId);
        doc.append("createUserName", userName);
        doc.append("_updateUserId", _userId);
        doc.append("updateUserName", userName);
        return insertOne(doc);
    }

    /**
     * 审批单据
     *
     * @param _userId 用户ID
     * @param userName 用户名
     * @param billNo 数据编号
     */
    public void audit(ObjectId _userId, String userName, String billNo) {
        updateOne(Criteria.where("billNo").is(billNo),
                new Document("status", BillStatus.Audit.toString())
                        .append("auditTime", new Date())
                        .append("_auditUserId", _userId)
                        .append("auditUserName", userName));
    }

    /**
     * 反审批单据
     *
     * @param billNo 数据编号
     */
    public void unaudit(String billNo) {
        updateOne(Criteria.where("billNo").is(billNo), new Document("status", BillStatus.Save.toString()));
    }

    /**
     * 获取单据详细
     *
     * @param cond 检索条件
     */
    public Iterator<Document> findShowList(Criteria cond, Document sort, int skip, int limit) {
        List<Document> docList = new ArrayList<>();
        docList.add(new Document("$match", cond.getCriteriaObject()));
        docList.add(new Document("$sort", sort));
        docList.add(new Document("$skip", skip));
        docList.add(new Document("$limit", limit));
        docList.add(new Document("$lookup", new Document("from", PurOrderDetailDao.NAME).append("foreignField", "orderNo").append("localField", "orderNo").append("as", "detail")));
        return this.aggregate(docList);
    }

    /**
     * 获取单据详细
     *
     * @param cond 检索条件
     */
    public Iterator<Document> findPurOrderList(Criteria cond) {
        List<Document> docList = new ArrayList<>();
        docList.add(new Document("$match", cond.getCriteriaObject()));
        docList.add(new Document("$lookup", new Document("from", PurOrderDetailDao.NAME).append("foreignField", "orderNo").append("localField", "orderNo").append("as", "detail")));
        docList.add(new Document("$unwind", new Document("path", "$detail").append("preserveNullAndEmptyArrays", true)));
        docList.add(new Document("$lookup", new Document("from", PurSettlementDao.NAME).append("foreignField", "_detailId").append("localField", "detail._id").append("as", "settlement")));
        docList.add(new Document("$unwind", new Document("path", "$settlement").append("preserveNullAndEmptyArrays", true)));
        docList.add(new Document("$sort", new Document("createTime", -1).append("detail._id", 1).append("settlement.create", -1)));
        return this.aggregate(docList);
    }

    //用于生产任务分页
    public Iterator<Document> findPrdTask(Criteria cond, Document sort, int pageIndex, int pageSize) {
        List<Document> docList = new ArrayList<>();
        docList.add(new Document("$match", cond.getCriteriaObject()));
        if (sort != null) {
            docList.add(new Document("$sort", sort));
        }
        if (pageIndex > 0 && pageSize > 0) {
            docList.add(new Document("$skip", pageIndex * pageSize));
        }
        if (pageSize > 0) {
            docList.add(new Document("$limit", pageSize));
        }
        return this.aggregate(docList);
    }
    //计算生产任务总数量
    public int countFindPrdTask(Criteria cond) {
        List<Document> docList = new ArrayList<>();
        docList.add(new Document("$match", cond.getCriteriaObject()));
        docList.add(new Document("$group",
                new Document("_id", null).append("count", new Document("$sum", 1))));
        Iterator<Document> itr = this.aggregate(docList);
        if (itr.hasNext()) {
            return VtuzxUtil.getObject(itr.next(), "count");
        }
        return 0;
    }

}
